package com.meowcook.service.impl;

import com.meowcook.common.AppResult;
import com.meowcook.common.ResultCode;
import com.meowcook.dao.MembersMapper;
import com.meowcook.dao.MessageMapper;
import com.meowcook.dao.SessionMapper;
import com.meowcook.exception.AppException;
import com.meowcook.mapper.SessionPojoMapper;
import com.meowcook.pojo.dto.SessionDTO;
import com.meowcook.pojo.po.Members;
import com.meowcook.pojo.po.Message;
import com.meowcook.pojo.po.Session;
import com.meowcook.pojo.vo.MessageVO;
import com.meowcook.pojo.vo.SessionVO;
import com.meowcook.service.IMessageService;
import com.meowcook.service.ISessionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 28792
 * Date: 2025-11-15
 * Time: 12:13
 */
@Slf4j
@Service
public class SessionServiceImpl implements ISessionService {
    @Resource
    private SessionMapper sessionMapper;
    @Resource
    private MembersMapper membersMapper;
    @Resource
    private MessageMapper messageMapper;
    @Resource
    private SessionPojoMapper sessionPojoMapper;
    @Resource
    private IMessageService messageService;
    @Override
    public Long createSession(SessionDTO sessionDTO) {
        ArrayList<Long> userIdList = sessionDTO.getUserId();
        Session session = new Session();
        session.setName(sessionDTO.getName());
        session.setAvatarUrl(sessionDTO.getAvatarUrl());
        session.setNumMember(userIdList.size());
        session.setIsDeleted((byte) 0);
        session.setCreateTime(new Date());
        session.setUpdateTime(new Date());
        int row = sessionMapper.insert(session);
        if (row != 1) {
            throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
        }

        for (Long userId : userIdList) {
            Members members = new Members();
            members.setUserId(userId);
            members.setSessionId(session.getId());
            members.setInfoRecord("[]");
            members.setUncheckNum(0);
            members.setIsAssociated((byte) 0);
            members.setCreateTime(new Date());
            members.setUpdateTime(new Date());
            int r = membersMapper.insert(members);
            if (r != 1) {
                throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
            }
        }
        return session.getId();
    }

    @Override
    public ArrayList<SessionVO> getSessionList(Long userId) {
        // 获取会话列表
        ArrayList<Session> sessions = sessionMapper.selectByUserId(userId);
        ArrayList<SessionVO> sessionVOs = sessionPojoMapper.toVO(sessions);

        // 构造会话的最新一条消息,未读消息数和会话信息Id数组
        for (SessionVO session : sessionVOs) {
            // 获取会话信息Id数组
            ArrayList<Long> messageIdList = messageService.getMessageIdList(userId, session.getId());
            ArrayList<Long> lastMessageId = new ArrayList<>();

            // 如果信息Id数组为空，则设置为空数组
            if (messageIdList.isEmpty()) {
                session.setMessageIdList(new ArrayList<>());
                session.setLastMessage(null);
                session.setUncheckNum(0);
            } else {
                // 获取最新一条消息
                lastMessageId.add(messageIdList.get(messageIdList.size()-1));
                ArrayList<Message> messages = messageMapper.selectByIdList(lastMessageId);
                MessageVO messageVO = sessionPojoMapper.toVO(messages.get(0));
                // 获取未读消息数
                Members members = membersMapper.selectUncheckNum(userId, session.getId());
                // 设置
                session.setMessageIdList(messageIdList);
                session.setLastMessage(messageVO);
                session.setUncheckNum(members.getUncheckNum());
            }
        }
        return sessionVOs;
    }
}
